package com.lrs.java.jdbc.jdbc_exercise;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 用变量来记录insert语句中要插入的值
 * @author Administrator
 *
 */
public class TestInsert_Variant {
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		
		if(args.length != 3) {
			System.out.println("error parameter!");
			return;
		}
		
		int  empno = 0;
		String ename = args[1];
		int sal = 0;
		
		try {
			empno = Integer.parseInt(args[0]);
		} catch(NumberFormatException e) {
			System.out.println("the first word must be number type");
		}
		try {
			sal = Integer.parseInt(args[2]);
		} catch(NumberFormatException e) {
			System.out.println("the second parameter must be number type");
		}
		
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ZLPD", "scott", "tiger");
			stmt = conn.createStatement();
			
			//String sql = "insert into emp2(empno, ename, sal) values(9997, 'zl', 3000)";
			
			String sql = "insert into emp2(empno, ename, sal) values(" + empno + ",'" + ename + "'," +  sal + ")";
			
			System.out.println(sql);
			
			stmt.executeUpdate(sql);
			
			System.out.println("success!");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			
			try {
				if(stmt != null) {
					stmt.close();
					stmt = null;
				}		
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null) {
					conn.close();
					conn = null;
				}		
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
			
	}
}
